package top.soulblack.purchasesale.controller.setting;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.GoodsBrand;
import top.soulblack.purchasesale.model.GoodsSize;
import top.soulblack.purchasesale.service.GoodsBrandService;

import static top.soulblack.purchasesale.common.CodeMsg.*;

/**
 * create by 小川
 */

@Controller
@RequestMapping("/brand")
public class GoodsBrandController {

    @Autowired
    private GoodsBrandService goodsBrandService;

    @ResponseBody
    @RequestMapping("/addBrand")
    public Result<Boolean> addBrand(GoodsBrand goodsBrand) {
        // 判断是否存在当前品牌
        if (goodsBrandService.existBrand(goodsBrand.getName())) {
            return Result.error(BRAND_EXIST);
        }
        // 判断排序数是否在1到100区间
        if (goodsBrand.getSort() > 100 || goodsBrand.getSort() < 1) {
            return Result.error(SORT_ERROR);
        }
        goodsBrandService.addBrand(goodsBrand);
        return Result.success(true);
    }

    @ResponseBody
    @RequestMapping("/editBrand")
    public Result<Boolean> editBrand(GoodsBrand goodsBrand) {
        System.out.println(goodsBrand.getId());
        if (goodsBrandService.updateBrand(goodsBrand)) {
            return Result.success(true);
        }
        return Result.error(BRAND_EDIT_ERROR);
    }

    @ResponseBody
    @RequestMapping("/deleteBrand")
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deleteBrand(int brandId) {
        System.out.println(brandId);
        if (goodsBrandService.selectBrand(brandId)){
            return Result.error(MATERIAL_LIFE);
        }
        if (goodsBrandService.deleteBrand(brandId)) {
            return Result.success(true);
        }
        return Result.error(BRAND_DELETE);
    }
}
